import {
  baseurl
} from '../common/config.js'
export function uploadQ(filePath) {
  if (filePath == '' || filePath.indexOf('xl-cover.kongetech.cn') != -1) {
    console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    Promise.resolve('');
  } else {
    return uploadFile('/common/uploadQ', filePath)
  }

}

export function uploadSound(filePath) {
  return uploadFile('/common/uploadSound', filePath)
}

export function uploadAvatr(filePath, userId) {
  console.log('userId', userId)
  return uploadFile('/system/user/appAvatar', filePath, {
    userId: userId
  })
}

function uploadFile(url, filePath, formData = {}) {
  return new Promise((resolved, rejected) => {
    if (filePath == '' || filePath.indexOf('xl-cover.kongetech.cn') != -1) {
      debugger
      resolved('')
      return;
    }
    uni.uploadFile({
      url: baseurl + '' + url,
      filePath: filePath,
      formData: formData,
      name: 'file',
      success: (res) => {
        if (res) {
          try {
            let data = JSON.parse(res.data)
            if (data.code == 200) {
              if (url == '/common/uploadSound') {
                resolved(data.url)
              } else {
                resolved(data.msg)
              }

            } else {
              uni.showToast({
                icon: 'none',
                title: data.msg,
                duration: 2000
              })
              rejected(false)
            }
          } catch (e) {
            console.error(e)
          }

        }
      },
      fail: (err) => {
        console.log(err)
        resolved("")
      }
    })
  })
}